/*
 * Copyright (c) 2016-8-31
 * created by alex
 */

package com.quanquan.common;


public class Response {

    private static final String OK = "ok";
    private static final String ERROR = "error";
    private static final int SUCCESS_CODE = 1000;
    private static final int FAILURE_CODE = 1001;

    private Meta meta;
    private Object data;

    public Response success() {
        this.meta = new Meta(true, OK, SUCCESS_CODE);
        return this;
    }

    public Response success(Object data) {
        this.meta = new Meta(true, OK, SUCCESS_CODE);
        this.data = data;
        return this;
    }

    public Response failure() {
        this.meta = new Meta(false, ERROR, FAILURE_CODE);
        return this;
    }

    public Response failure(String message) {
        this.meta = new Meta(false, message, FAILURE_CODE);
        return this;
    }

    public Response failure(Object data) {
        this.meta = new Meta(false, ERROR, FAILURE_CODE);
        this.data = data;
        return this;
    }

    public Response failure(String message, int code) {
        this.meta = new Meta(false, message, code);
        return this;
    }

    public Meta getMeta() {
        return meta;
    }

    public Object getData() {
        return data;
    }

    public class Meta {

        private boolean success;
        private String message;
        private int code;

        public Meta(boolean success, int code) {
            this.success = success;
            this.code = code;
        }

        public Meta(boolean success, String message, int code) {
            this.success = success;
            this.message = message;
            this.code = code;
        }

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }
}
